#include <Arduino.h>
#include "com/communication.h"
#include "message.h"
#include "protocol.h"


volatile bool button_pressed = false;
extern volatile bool received_msg;
extern volatile bool interrupt;

void pair_with_slave();

int main(void) {
	init();

	setup();

	for (;;)
		loop();

	return 0;
}



void button_interrupt() {
	button_pressed = true;
}

void setup() {
	//pinMode(2, INPUT); // RF24 Interrupt pin
	pinMode(3, INPUT); // Button Interrupt pin
	digitalWrite(3, HIGH);

	Serial.begin(9600);
	Serial.println("I\'m the master!");

	attachInterrupt(1, button_interrupt, FALLING);

	com_activate_debug();
	com_init_master();

	delay(500);
}

void loop() {
	//Serial.println("Loop!");
	delay(100);

	/*if(com_send_msg_to_slave(message(0, PAIRING), 0))
		Serial.println("Message sent!");
	else
		Serial.println("Could not send message");
	*/
	/*if(com_available()) {
		Serial.println("Message available!");
		message msg = com_read_msg_from_slave(0, 200);
		if(msg.getComType() != ERROR)
			Serial.println("Received a message!");
	}*/

	if(button_pressed) { // Pairing
		Serial.println("Start pairing...");
		pair_with_slave();
		button_pressed = false;
	}
	/*
	if(com_available()) {

	}*/
}

void pair_with_slave() {
	if(com_pair_with_slave())
		Serial.println("Successfully paired with slave");
}
